﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Button[,] botoes;
        String Texto = "X";

        public Form1()
        {
            botoes = new Button[3, 3];  

            InitializeComponent();

            botoes[0, 0] = button1;
            botoes[1, 0] = button2;
            botoes[2, 0] = button3;

            botoes[0, 1] = button4;
            botoes[1, 1] = button5;
            botoes[2, 1] = button6;

            botoes[0, 2] = button7;
            botoes[1, 2] = button8;
            botoes[2, 2] = button9;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Button botao = (Button)sender;


            if (botao.Text.Length == 0)
            {
                botao.Text = Texto;

                if (Texto.Equals("X"))
                {
                    checkBox1.Checked = false;
                    checkBox2.Checked = true;
                    botao.Enabled = false;
                    Texto = "O";
                }
                else
                {
                    Texto = "X";
                    checkBox1.Checked = true;
                    checkBox2.Checked = false;
                    botao.Enabled = false;
                }
            }

            //Testando vencedores
            String winner = getWinner();
            if (winner.Length > 0)
            {
                MessageBox.Show("O vencedor é " + winner);
                InicioDeJogo();
            }
            else
            {
                if (!emBranco())
                {
                    MessageBox.Show("Deu Velha!");
                    InicioDeJogo();
                }
            }
        }

        private String getWinner()
        {

            //Checar horizontalmente
            for (int i = 0; i < 3; i++) 
            {
                if (botoes[0,i].Text.Length > 0 && 
                    botoes[0,i].Text.Equals(botoes[1,i].Text) &&
                    botoes[1,i].Text.Equals(botoes[2,i].Text))
                {
                    return botoes[0,i].Text;
                }
            }

            //Checar verticalmente
            for (int i = 0; i < 3; i++)
            {
                if (botoes[i, 0].Text.Length > 0 &&
                    botoes[i, 0].Text.Equals(botoes[i, 1].Text) &&
                    botoes[i, 1].Text.Equals(botoes[i, 2].Text))
                {
                    return botoes[i, 0].Text;
                }
            }

            //Diagonais
            if (botoes[0, 0].Text.Length > 0 &&
                botoes[0, 0].Text.Equals(botoes[1, 1].Text) &&
                botoes[1, 1].Text.Equals(botoes[2, 2].Text))
            {
                return botoes[0, 0].Text;
            }

            if (botoes[0,2].Text.Length > 0 &&
                botoes[0,2].Text.Equals(botoes[1,1].Text) &&
                botoes[1,1].Text.Equals(botoes[2,0].Text))
            {
                return botoes[0, 2].Text;
            }

            return "";
        }

        private bool emBranco()
        {
            for (int y = 0; y < 3; y++)
            {
                for (int x = 0; x < 3; x++)
                {
                    if (botoes[x,y].Text.Length == 0)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button10_Click(object sender, EventArgs e)
        {
            InicioDeJogo();
        }

    }
}
